package com.zbhk.qc.service.impl;

import com.zbhk.qc.dto.BaseResponseEntity;
import com.zbhk.qc.entity.VdAdClick;
import com.zbhk.qc.entity.VdAdClickDetail;
import com.zbhk.qc.mapper.VdAdClickDetailMapper;
import com.zbhk.qc.mapper.VdAdClickMapper;
import com.zbhk.qc.service.VdAdClickDetailService;
import com.zbhk.qc.util.TimeTransformUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Lazy(false)
@Service
@Slf4j
public class VdAdClickDetailServiceImpl implements VdAdClickDetailService {

    @Autowired
    private VdAdClickDetailMapper vdAdClickDetailMapper;

    @Autowired
    private VdAdClickMapper vdAdClickMapper;


    @Override
    public BaseResponseEntity addClickNun(String uuid) {
        BaseResponseEntity baseResponseEntity = new BaseResponseEntity();
        baseResponseEntity.setSuccess(true);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("inviteCode", uuid);
        List<VdAdClick> vdAdClickList = vdAdClickMapper.selectByMap(map1);
        if (vdAdClickList == null || vdAdClickList.size() == 0) {
            baseResponseEntity.setSuccess(false);
            baseResponseEntity.setErrorMessage("无此用户！");
            return baseResponseEntity;
        }

        String clickDate = TimeTransformUtil.date2string(new Date());
        Map<String, Object> map2 = new HashMap<>();
        map2.put("acId", vdAdClickList.get(0).getId());
        map2.put("clickDate", clickDate);
        List<VdAdClickDetail> vdAdClickDetailList = vdAdClickDetailMapper.selectByMap(map1);
        if (vdAdClickDetailList == null || vdAdClickDetailList.size() == 0) {
            VdAdClickDetail vdAdClickDetail = new VdAdClickDetail();
            vdAdClickDetail.setAcId(vdAdClickList.get(0).getId());
            vdAdClickDetail.setClickNum(Long.valueOf(1));
            vdAdClickDetail.setClickDate(clickDate);
            vdAdClickDetailMapper.insert(vdAdClickDetail);
        }else {
            VdAdClickDetail vdAdClickDetail = vdAdClickDetailList.get(0);
            vdAdClickDetail.setClickNum(vdAdClickDetail.getClickNum() + 1L );
            vdAdClickDetailMapper.updateById(vdAdClickDetail);
        }
        return baseResponseEntity;
    }
}

  